package sqlexplorer.ui.views;

import java.security.InvalidParameterException;

import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import sqlexplorer.application.ConnectionsController;
import sqlexplorer.domain.DatabaseConnection;
import sqlexplorer.domain.DatabaseType;

public class EditConnectionDialog extends TitleAreaDialog {

	private Combo m_comboDatabase;
	private Text m_txtURL;
	private Text m_txtUser;
	private Text m_txtPassword;
	private Text m_txtConnectionName;
	private int m_index;
	
	private Shell _shell;
	public EditConnectionDialog(Shell parentShell, int index) {
		super(parentShell);
		m_index = index;
		_shell = parentShell;
	}
	
	@Override
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setTitle("Edit connection");
		setMessage("Please edit your connection", IMessageProvider.INFORMATION);
		setTitleImage(new Image(parent.getDisplay(), getClass().getResourceAsStream("../../../../icons/database.png")));
		return contents;
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		parent.setLayout(layout);
		
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		
		Label lbDatabase = new Label(parent, SWT.NONE);
		lbDatabase.setText("Database");
		m_comboDatabase = new Combo(parent, SWT.READ_ONLY);
		
		DatabaseType[] types=DatabaseType.values();
		
		for(int i=0; i<types.length; i++){
			m_comboDatabase.add(types[i].toString());
		}
		
		Label lbConnectionName = new Label(parent, SWT.NONE);
		lbConnectionName.setText("Connection name");
		m_txtConnectionName = new Text(parent, SWT.BORDER);
		m_txtConnectionName.setLayoutData(gridData);
		Label lblURL = new Label(parent, SWT.NONE);
		lblURL.setText("JDBC URL");
		m_txtURL = new Text(parent, SWT.BORDER);
		m_txtURL.setLayoutData(gridData);
		Label lblUser = new Label(parent, SWT.NONE);
		lblUser.setText("User");
		m_txtUser = new Text(parent, SWT.BORDER);
		m_txtUser.setLayoutData(gridData);
		Label lblPasswor = new Label(parent, SWT.NONE);
		lblPasswor.setText("Password");
		m_txtPassword = new Text(parent, SWT.BORDER | SWT.PASSWORD);
		m_txtPassword.setLayoutData(gridData);
		
		DatabaseConnection databaseconnection = ConnectionsController.getInstance().getDatabaseConnection(m_index);
		setSelectedDatabase(databaseconnection.getDatabaseType());
		setConnectionName(databaseconnection.getConnectionName());
		setURL(databaseconnection.getJdbcUrl());
		setUser(databaseconnection.getUserName());
		setPassword(databaseconnection.getPassword());
		
		return parent;
	}

	
	@Override
	protected void okPressed() {
		try {
			ConnectionsController.getInstance().editConnection(m_index, getURL(), 
					getConnectionName(), getUser(), getPassword(), getSelectedDatabase());
			super.okPressed();
		}catch(InvalidParameterException ex) {
			MessageBox msgBox=new MessageBox(_shell ,SWT.ICON_WARNING|SWT.OK);
			msgBox.setText("Warning");
			msgBox.setMessage("Please enter valid values.");
			msgBox.open();
		}
	}
	
	public String getConnectionName() {
		return m_txtConnectionName.getText();
	}
	
	public void setConnectionName(String connectionName) {
		m_txtConnectionName.setText(connectionName);
	}
	
	public DatabaseType getSelectedDatabase() {
		String selectedDatabase = (String)m_comboDatabase.getItem(m_comboDatabase.getSelectionIndex());
		return DatabaseType.valueOf(DatabaseType.class, selectedDatabase.toUpperCase());
	}
	
	public void setSelectedDatabase(DatabaseType selectedDatabase) {
		String database = selectedDatabase.toString();
		String [] arr= m_comboDatabase.getItems();
		for(int i = 0; i < arr.length; i++) {
			if(arr[i].equals(database)) {
				m_comboDatabase.select(i);
			}
		}
	}
	
	public String getURL() {
		return m_txtURL.getText();
	}
	
	public void setURL(String url) {
		m_txtURL.setText(url);
	}
	
	public String getUser() {
		return m_txtUser.getText();
	}
	
	public void setUser(String user) {
		m_txtUser.setText(user);
	}
	
	public String getPassword() {
		return m_txtPassword.getText();
	}
	 
	public void setPassword(String password) {
		m_txtPassword.setText(password);
	}
}
